package com.flyee.viewer.pack.helper
{	
	import com.flyee.events.LoaderEvent;
	import com.flyee.events.SelectionEvent;
	import com.flyee.utils.EventManager;
	import com.flyee.utils.IconLoader;
	import com.flyee.viewer.Destroy;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import mx.core.Application;
	import mx.core.UIComponent;

	public class ItemViewModel extends UIComponent implements Destroy
	{
		private const WIDTH_ICON:int = 40;
		private const HEIGHT_ICON:int = 40;
		/**
		 * ICON数据的备份
		 */
		private var bitdata:BitmapData = null;
		/**
		 * 道具ICON
		 */
		private var bitmap:Bitmap = null;
		private var using:Boolean = false;
		protected var _dataModel:ItemDataModel = null;
		public static var dragSprite:UIComponent = null;
		public static var canShowInfo:Boolean = true;
		public var myTimer:Timer = null;
		public var DELAY_TIME:Number = 300;
	
		public function ItemViewModel()
		{
			_dataModel = new ItemDataModel();
			this.toolTip = this.dataModel.name;
			bitmap = new Bitmap();
			this.addChild(bitmap);
		}

		private function getDragSprite():Sprite
		{

			if (dragSprite == null)
			{
				dragSprite = new UIComponent();
				dragSprite.mouseChildren = false;
				dragSprite.mouseEnabled = false;
			}

			if (!Application.application.contains(dragSprite))
			{
				Application.application.addChild(dragSprite)
			}
			return dragSprite;
		}

		private function getTimer():Timer
		{

			if (myTimer == null)
			{
				myTimer = new Timer(DELAY_TIME);
				myTimer.addEventListener(TimerEvent.TIMER, timerHandle);
			}
			return myTimer;
		}

		private function init():void
		{

		}

		private function addListeners():void
		{
			isDrag = true;
			this.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			
		}

		private function removeListeners():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			this.removeEventListener(MouseEvent.MOUSE_OVER, overHandler);
		}

		private function timerHandle(e:TimerEvent):void
		{
			this.dispatchEvent(new SelectionEvent(SelectionEvent.DRAG_PROP, this.dataModel.id, this.dataModel.name, this));
			EventManager.dispatherTagEvent(new SelectionEvent(SelectionEvent.DRAG_PROP, this.dataModel.id, this.dataModel.name, this));
		}

		private function startDragHandler(e:MouseEvent):void
		{
			getTimer().start();
			canShowInfo = false;
			//TipsWindow.getInstance().clear();
			if (!using)
			{
				var tempSprite:Sprite = this.getDragSprite();
				tempSprite.x = Application.application.stage.mouseX - 21;
				tempSprite.y = Application.application.stage.mouseY - 21;
				tempSprite.startDrag();

				while(tempSprite.numChildren > 0)
				{
					var tempDisplay:DisplayObject = tempSprite.removeChildAt(0)

					if (tempDisplay is Bitmap)
					{
						var tempBitmap:Bitmap = tempDisplay as Bitmap;
						tempBitmap.bitmapData = null;
					}
				}
				var tempBitmap2:Bitmap = new Bitmap(this.icon.clone());
				tempBitmap2.width = 42;
				tempBitmap2.height = 42;
				tempSprite.addChild(tempBitmap2);

				Application.application.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			}
		}

		private function stopDragHandler(e:MouseEvent):void
		{
			getTimer().stop();
			canShowInfo = true;
			var tempSprite:Sprite = this.getDragSprite();

			while(tempSprite.numChildren > 0)
			{
				var tempDisplay:DisplayObject = tempSprite.removeChildAt(0)

				if (tempDisplay is Bitmap)
				{
					var tempBitmap:Bitmap = tempDisplay as Bitmap;
					tempBitmap.bitmapData = null;
				}
			}
			Application.application.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			this.dispatchEvent(new SelectionEvent(SelectionEvent.STOPMOVE_PROP, this.dataModel.id, this.dataModel.name, this));
		}

		private function overHandler(e:MouseEvent):void
		{

			if (canShowInfo)
			{
				var goldCoin:int = _dataModel.priceGoldCoin;
				var blueCoin:int = _dataModel.priceCityCoin;
				//TipsWindow.getInstance().showMessage(this._dataModel.tipMessage, this._dataModel.stackCount, goldCoin, blueCoin, dataModel.packID, dataModel.isDIY);
			}
		}

		private function outHandler(e:MouseEvent):void
		{
		//TipsWindow.getInstance().clear();
		}

		/**
		 *
		 * @param e
		 *
		 */
		private function completeHandler(event:LoaderEvent):void
		{
			/* bitdata = event.getBitmapData();
			bitmap.bitmapData = bitdata; */
			icon = event.getBitmapData();
			var load:IconLoader = event.currentTarget as IconLoader;
			load.removeEventListener(LoaderEvent.COMPLETE_LOADER, completeHandler);
			load.clear();
			load = null; 
		}

		/**
		 *
		 * @param color
		 *
		 */
		private function applyFilter(color:int):void
		{
			switch(color)
			{
				case ItemDataModel.COLOR_GREEN_INT:
					return ;
				case ItemDataModel.COLOR_PURPLE_INT:
					return ;
				case ItemDataModel.COLOR_YELLOW_INT:
					return ;
				default:
					return ;
			}
		}
		
		public function set isDrag(b:Boolean):void{
			if(b){
				this.buttonMode = true;
				this.useHandCursor = true;
				this.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			}else{
				this.buttonMode = false;
				this.useHandCursor = false;
				this.removeEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			}
		}

		public function loadIcon():void
		{
			if (dataModel.iconPath)
			{
				//var url:String =  ChannelConfig.DOWNLOAD_FILE + dataModel.iconPath;
				//var load:IconLoader = new IconLoader(url);
				//load.addEventListener(LoaderEvent.COMPLETE_LOADER, completeHandler);
			}else{
				//icon = PlatformDepot.getInstance().getBitmapDataByName("TempIcon");
			}
		}
		public function get itemBitmap():Bitmap{
			return bitmap;
		}

		public function set icon(data:BitmapData):void
		{
			bitdata = data.clone();
			if(bitmap){
				bitmap.bitmapData = data;
			}
			bitmap.scaleX = (this.WIDTH_ICON - 2) / bitmap.width;
			bitmap.scaleY = (this.HEIGHT_ICON - 2) / bitmap.height;
			addListeners();
		}

		public function get icon():BitmapData
		{
			return this.bitmap.bitmapData.clone();
		}

		public function showIcon(b:Boolean):void
		{

			if (b)
			{
				bitmap.bitmapData = bitdata;
			}
			else
			{
				bitmap.bitmapData = null;
			}
		}

		public function set location(loc:Point):void
		{
			this.x = loc.x;
			this.y = loc.y;
		}

		public function get propIcon():BitmapData
		{
			return bitdata;
		}

		public function isUsing():Boolean
		{
			return using
		}

		public function setUsing(b:Boolean):void
		{
			using = b;
		}

		public function get dataModel():ItemDataModel
		{
			return _dataModel;
		}
		[Inspectable(category="General")]
		public function set data(model:Object):void
		{	
			if(model){
				_dataModel = model as ItemDataModel;
				loadIcon();
			}
		}


		public function dispose():void
		{
			removeListeners();

			if (this.bitmap != null)
			{
				if(this.owns(bitmap)){
					this.removeChild(bitmap);
				}
				
				if (this.bitmap.bitmapData != null)
				{
					this.bitmap.bitmapData.dispose();
					this.bitmap.bitmapData = null;
				}
			}

			this.bitmap = null;

			if (this.bitdata != null)
			{
				this.bitdata.dispose();
				this.bitdata = null;
			}

			this.graphics.clear();

		}
	}
}



